#pragma once
#include <iostream>
#include <memory>
#include <mutex>

//利用CTRP实现单例模式

template <typename T>
class Singleton
{
public:
    //返回单例对象的引用
    static std::shared_ptr<T> getInstance()
    {
        static std::once_flag flag;
        std::call_once(flag, [&]() {
            _instance = std::shared_ptr<T>(new T());
        });
        return _instance;
    }


protected:
    Singleton()=default; //构造函数
    ~Singleton()=default; //析构函数
    Singleton(const Singleton<T>& other)=delete;
    Singleton<T>& operator=(const Singleton<T>& other)=delete;

private:
    static std::shared_ptr<T> _instance; //单例对象

};
//记住了静态成员变量必须在类外初始化
template <typename T>
std::shared_ptr<T> Singleton<T>::_instance = nullptr;
